package com.fsh.controller;

import com.fsh.pojo.PageBean;
import com.fsh.pojo.Result;
import com.fsh.pojo.User;
import com.fsh.service.UserService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/list")
    public Result list(){
        List<User> userList = userService.queryAll();
        if(userList==null){
            return Result.error("没有数据");
        }
        return Result.success(userList);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user){
        userService.addOne(user);
        return Result.success(user);
//        System.out.println(user);
//        System.out.println("是新增操作");
//        return Result.success();
    }

    @DeleteMapping("/del")
    public Result del(Integer id){
        Integer count = userService.delById(id);
        return count>0?Result.success():Result.error("删除失败");
    }

    @PutMapping("/modify")
    public Result modify(@RequestBody User user){

        userService.updateById(user);
//        System.out.println(user);
        System.out.println("是更新操作");

        return Result.success(user);
    }

    @GetMapping("/pageList/{start}/{pageSize}")
    public Result pageList(@PathVariable("start") Integer start ,@PathVariable("pageSize") Integer pageSize){
        System.out.println(start+":"+pageSize);
        Page<User> p = userService.pageList(start, pageSize);
        PageBean pageBean=new PageBean();
        pageBean.setData(p.getResult());
        pageBean.setTotal(p.getTotal());
        return Result.success(pageBean);
    }

}
